package org.reenscape.updater;
import org.reenscape.*;
import org.reenscape.model.*;
/**
* @author Diablo1123
* Handles spells and graphics.
*/
public class Graphics {

	public static void createProjectile(int casterY, int casterX, int offsetY, int offsetX, int angle, int speed, int gfxMoving, int startHeight, int endHeight, int MageAttackIndex) {
		int calcX = 0;
		int calcY = 0;
		for (int i = 0; i < ClientUpdater.players.length; i++) {
			if (ClientUpdater.players[i] != null) {
				PacketParser p3 = (PacketParser) ClientUpdater.players[i];
				calcX = casterX - p3.absX;
				calcY = casterY - p3.absY;
				if (calcX >= -16 && calcX <= 15 && calcY >= -16 && calcY <= 15) {
					p3.outStream.createFrame(85);
					p3.outStream.writeByteC((casterY - (p3.mapRegionY * 8)) - 2);
					p3.outStream.writeByteC((casterX - (p3.mapRegionX * 8)) - 3);
					p3.outStream.createFrame(117);
					p3.outStream.writeByte(angle); 
					p3.outStream.writeByte(offsetY);
					p3.outStream.writeByte(offsetX);
					p3.outStream.writeWord(MageAttackIndex);
					p3.outStream.writeWord(gfxMoving); 
					p3.outStream.writeByte(startHeight); 
					p3.outStream.writeByte(endHeight);
					p3.outStream.writeWord(51); 
					p3.outStream.writeWord(speed);
					p3.outStream.writeByte(16);
					p3.outStream.writeByte(64);
				}
			}
		}
	}
	
	public static void StillGFX(int X, int Y, int ID, int Height){
		int calcX = 0;
		int calcY = 0;
		for (int i = 0; i < ClientUpdater.players.length; i++) {
			if (ClientUpdater.players[i] != null) {
				PacketParser p3 = (PacketParser) ClientUpdater.players[i];
				calcX = X - p3.absX;
				calcY = Y - p3.absY;
				if (calcX >= -16 && calcX <= 15 && calcY >= -16 && calcY <= 15 && p3.heightLevel == Height) {
					p3.outStream.createFrame(85);
					p3.outStream.writeByteC(Y - (p3.mapRegionY * 8));
					p3.outStream.writeByteC(X - (p3.mapRegionX * 8));
					p3.outStream.createFrame(4);
					p3.outStream.writeByte(0);//Tiles away (X >> 4 + Y & 7)
					p3.outStream.writeWord(ID);//Graphic id
					p3.outStream.writeByte(100);//height of the spell above it's basic place, i think it's written in pixels 100 pixels higher
					p3.outStream.writeWord(0);//Time before casting the graphic
				}
			}
		}
	}
	
	public static void AttackNPC(int npcSlot, PacketParser p3, int damage, double exp, int startID, int movegfxID, int endgfx){
		StillGFX(p3.absX, p3.absY, startID, p3.heightLevel);
		int till = timer + 4;
		if (till > 1000) {
			till -= 1000;
		}
		//while (till != timer);
		int offsetY = (p3.absY - NpcUpdater.npcs[npcSlot].absY) * -1;
		int offsetX = (p3.absX - NpcUpdater.npcs[npcSlot].absX) * -1;
		createProjectile(p3.absY, p3.absX, offsetX, offsetY, 50, 90, movegfxID, 100, 100, npcSlot);
		till = timer + 8;
		if (till > 1000) {
			till -= 1000;
		}
		int hitDiff = damage;
		int EnemyHP = NpcUpdater.npcs[npcSlot].HP;
		if ((EnemyHP - hitDiff) < 0) {
			hitDiff = EnemyHP;
		}
		NpcUpdater.npcs[npcSlot].hitDiff += hitDiff;
		NpcUpdater.npcs[npcSlot].Killing[p3.playerId] += hitDiff;
		NpcUpdater.npcs[npcSlot].updateRequired = true;
		NpcUpdater.npcs[npcSlot].hitUpdateRequired = true;
		p3.addSkillXP(exp, 6);
		StillGFX(NpcUpdater.npcs[npcSlot].absX, NpcUpdater.npcs[npcSlot].absY, endgfx, p3.heightLevel);
	}
	
	public static void AttackPlayer(int attackedID, PacketParser p3, int damage, int exp, int startID, int movegfxID, int endgfx){
		StillGFX(p3.absX, p3.absY, startID, p3.heightLevel);
		int till = timer + 4;
		if (till > 1000) {
			till -= 1000;
		}
		while (till != timer);
		int offsetY = (p3.absY - ClientUpdater.players[attackedID].absY) * -1;
		int offsetX = (p3.absX - ClientUpdater.players[attackedID].absX) * -1;
		createProjectile(p3.absY, p3.absX, offsetX, offsetY, 50, 90, movegfxID, 100, 100, attackedID * -1);
		till = timer + 8;
		if (till > 1000) {
			till -= 1000;
		}
		int hitDiff = damage;
		int EnemyHP = ClientUpdater.players[attackedID].playerLevel[3];
		ClientUpdater.players[attackedID].hitUpdateRequired = true;
		ClientUpdater.players[attackedID].updateRequired = true;
		ClientUpdater.players[attackedID].appearanceUpdateRequired = true;
		if ((EnemyHP - hitDiff) < 0) {
			hitDiff = EnemyHP;
		}
		ClientUpdater.players[attackedID].hitDiff += hitDiff;
		p3.addSkillXP(exp, 6);
		StillGFX(ClientUpdater.players[attackedID].absX, ClientUpdater.players[attackedID].absY, endgfx, p3.heightLevel);
	}
	public static void process(){
		timer++;
		if (timer > 1000) {
			timer -= 1000;
		}
	}
	
	public static int timer = 0;
}